﻿using System.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication
{
    public class FeedImage
    {

        public Uri ImageUrl { get { return this._imageurl; } set { this._imageurl = value; } }
        public string Title { get { return this._title; } set { this._title = value; } }
        public Uri Link { get { return this._link; } set { this._link = value; } }
        public string Description { get { return this._description; } set { this._description = value; } }
        public int Width { get { return this._width; } set { this._width = value; } }
        public int Height { get { return this._height; } set { this._height = value; } }

        private Uri _imageurl = null;
        private string _title = string.Empty;
        private Uri _link = null;
        private string _description = string.Empty;
        private int _width = 0;
        private int _height = 0;

        public FeedImage(Uri ImageUrl, string Title, Uri Link, string Description, int Width, int Height)
        {
            this._imageurl = ImageUrl;
            this._title = Title;
            this._link = Link;
            this._description = Description;
            this._width = Width;
            this._height = Height;
        }

        public FeedImage(string ImageUrl, string Title, string Link, string Description, int Width, int Height)
        {
            this._imageurl = new Uri(ImageUrl);
            this._title = Title;
            this._link = new Uri(Link);
            this._description = Description;
            this._width = Width;
            this._height = Height;
        }

        public FeedImage()
        {
            this._imageurl = null;
            this._title = string.Empty;
            this._link = null;
            this._description = string.Empty;
            this._width = 0;
            this._height = 0;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (this._imageurl == null) { return sb.ToString(); }

            sb.AppendLine("<image>");
            if (this._imageurl != null) { sb.AppendLine(string.Format("<url>{0}</url>", this._imageurl.OriginalString)); } else { sb.AppendLine("<url></url>"); }
            sb.AppendLine(string.Format("<title>{0}</title>", FeedHelper.XmlEncode(this._title)));
            if (this._link != null) { sb.AppendLine(string.Format("<link>{0}</link>", this._link.OriginalString)); } else { sb.AppendLine("<link></link>"); }
            sb.AppendLine(string.Format("<description>{0}</description>", FeedHelper.XmlEncode(this._description)));
            if (this._width > 0) { sb.AppendLine(string.Format("<width>{0}</width>", this._width)); }
            if (this._height > 0) { sb.AppendLine(string.Format("<height>{0}</height>", this._height)); }
            sb.AppendLine("</image>");
            return sb.ToString();
        }

        public XmlNode ToXml(XmlDocument Document)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, "image", "");

            if (this._imageurl != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "url", this._imageurl.OriginalString));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "url", string.Empty));
            }

            node.AppendChild(FeedHelper.CreateElement(Document, "title", this._title));

            if (this._link != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", this._link.OriginalString));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", string.Empty));
            }

            node.AppendChild(FeedHelper.CreateElement(Document, "description", this._description));

            if (this._width > 0) { node.AppendChild(FeedHelper.CreateElement(Document, "width", this._width.ToString())); }
            if (this._height > 0) { node.AppendChild(FeedHelper.CreateElement(Document, "height", this._height.ToString())); }

            return node;
        }
    }
}
